package cn.afactory.tygz.ui.base.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import com.baidu.navisdk.adapter.BNRoutePlanNode;
import com.baidu.navisdk.adapter.BaiduNaviManager;

import java.util.ArrayList;
import java.util.List;

import cn.afactory.tygz.TianYaGongZaiApplication;
import cn.afactory.tygz.model.NearByUserInfo;
import cn.afactory.tygz.model.SnsTopicInfo;
import cn.afactory.tygz.ui.PrivateMsgActivity;
import cn.afactory.tygz.ui.SysMessageListActivity;
import cn.afactory.tygz.ui.activities.ApartmentListActivity;
import cn.afactory.tygz.ui.BlackListActivity;
import cn.afactory.tygz.ui.activities.BNNavGuideActivity;
import cn.afactory.tygz.ui.activities.EntertainListActivity;
import cn.afactory.tygz.ui.activities.LifeServiceListActivity;
import cn.afactory.tygz.ui.MainActivity;
import cn.afactory.tygz.ui.MyFriendsActivity;
import cn.afactory.tygz.ui.activities.MyPublishSecondGoodsActivity;
import cn.afactory.tygz.ui.activities.RestaurantListActivity;
import cn.afactory.tygz.ui.activities.ScenicSpotsListActivity;
import cn.afactory.tygz.ui.activities.SecondGoodsAddActivity;
import cn.afactory.tygz.ui.activities.SecondGoodsListActivity;
import cn.afactory.tygz.ui.activities.TopicDetailActivity;
import cn.afactory.tygz.ui.sns.MyFollowSubjectActivity;
import cn.afactory.tygz.ui.sns.PublishSubjectActivity;
import cn.afactory.tygz.ui.sns.TopicListActivity;
import cn.afactory.tygz.ui.sns.UserListActivity;
import cn.afactory.tygz.ui.sns.UserProfileActivity;
import cn.afactory.tygz.ui.user.LoginActivity;
import cn.afactory.tygz.ui.user.UserInfoUpdateActivity;

/**
 * Created with Android Studio.
 * Title:NaviUtils
 * Description:
 * Copyright:Copyright (c) 2015
 * Company:和平必胜、正义必胜、人民必胜
 * Author:炎黄子孙
 * Date:15/9/11
 * Time:下午2:11
 * Version 1.0
 */
public class NaviUtils {
    /**
     * 登录页面
     * @param context
     */
    public static void gotoLogin(Context context){
        context.startActivity(new Intent(context, LoginActivity.class));
    }

    /**
     * 主界面
     * @param context
     */
    public static void gotoMain(Context context){
        context.startActivity(new Intent(context, MainActivity.class));
    }

    /**
     * 跳转到我的工友
     * @param context
     */
    public static void gotoMyFriends(Context context){
        context.startActivity(new Intent(context, MyFriendsActivity.class));
    }

    /**
     * 跳转到用户列表
     * @param context
     * @param userId
     * @param userName
     * @param userListType
     */
    public static void gotoUserList(Context context,
                                    long userId,
                                    String userName,
                                    long postId,
                                    int prasizeCount,
                                    int userListType){
        UserListActivity.gotoUserFansListPage(context, userId, userName, postId, prasizeCount, userListType);
    }

    /**
     * 跳转到我发布的二手物品列表页面
     * @param context
     */
    public static void gotoMyPublishSecondGoodsPage(Context context){
        MyPublishSecondGoodsActivity.gotoMyPublishSecondGoodsPage(context);
    }

    /**
     * 跳转到二手物品列表页面
     * @param context
     */
    public static void gotoSecondGoodsListPage(Context context){
        SecondGoodsListActivity.gotoSecondGoodsListPage(context);
    }

    /**
     * 跳转到餐饮列表页面
     * @param context
     */
    public static void gotoRestaurantListPage(Context context){
        RestaurantListActivity.gotoRestaurantListPage(context);
    }

    /**
     * 跳转到人才公寓列表页面
     * @param context
     */
    public static void gotoApartmentListPage(Context context){
        ApartmentListActivity.gotoApartmentListPage(context);
    }

    /**
     * 跳转到附近的娱乐场所列表页面
     * @param context
     */
    public static void gotoEntertainmentListPage(Context context){
        EntertainListActivity.gotoEntertainListPage(context);
    }

    /**
     * 跳转到周边景点的列表页面
     * @param context
     */
    public static void gotoScenicSpotsListPage(Context context){
        ScenicSpotsListActivity.gotoScenicSpotsListPage(context);
    }

    /**
     * 跳转到生活服务的列表页面
     * @param context
     */
    public static void gotoLifeServiceListPage(Context context){
        LifeServiceListActivity.gotoLifeServiceListPage(context);
    }

    /**
     * 跳转到黑名单设置页面
     * @param context
     */
    public static void gotoBlackListPage(Context context){
        BlackListActivity.gotoBlackListPage(context);
    }

    /**
     * 用户资料修改页
     * @param context
     */
    public static void gotoUserInfoUpdatePage(Context context){
        UserInfoUpdateActivity.gotoUserInfoUpdatePage(context);
    }

    public static void gotoMyFollowedSubjectListPage(Context context) {
        MyFollowSubjectActivity.gotoMyFollowedSubjectListPage(context);
    }

    /**
     * 话题列表页
     * @param context
     */
    public static void gotoTopicList(Context context){
        TopicListActivity.gotoTopicList(context);
    }

    public static void gotoSelectTopicId(Activity context){
        TopicListActivity.gotoSelectTopicId(context);
    }

    /**
     * 系统消息列表
     * @param context
     */
    public static void gotoSysMessageList(Context context){
        SysMessageListActivity.gotoSysMessageList(context);
    }

    /**
     * 跳转到私信详情列表
     * @param context
     * @param userId
     */
    public static void gotoMessageDetail(Context context,long userId,String nickName,String userIcon){
        PrivateMsgActivity.gotoMessageDetail(context, userId, nickName, userIcon);
    }

    /**
     * 跳转到个人主页
     * @param context
     * @param userId
     * @param nickName
     */
    public static void gotoUserProfileActivity(Context context,long userId,String nickName){
        UserProfileActivity.gotoUserProfile(context, userId, nickName);
    }

    /**
     * 跳转到二手物品发布页面
     * @param context
     */
    public static void gotoPublishSecondGoodsPage(Context context){
        SecondGoodsAddActivity.gotoAddSecondGoods(context);
    }

    public static void gotoWebView(Context context, String urlString) {
        Intent it = new Intent(Intent.ACTION_VIEW, Uri.parse(urlString));
        it.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
        context.startActivity(it);
    }

    /**
     * 跳转到话题详情页面
     */
    public static void gotoTopicDetailActivity(Context context, String topicTitle){
        TopicDetailActivity.gotoTopicDetailActivity(context, topicTitle);
    }

    /**
     * 跳转到话题详情页面
     */
    public static void gotoTopicDetailActivity(Context context, long topicId){
        TopicDetailActivity.gotoTopicDetailActivity(context, topicId);
    }

    /**
     * 跳转到话题详情页面
     */
    public static void gotoTopicDetailActivity(Context context,SnsTopicInfo snsTopicInfo){
        TopicDetailActivity.gotoTopicDetailActivity(context, snsTopicInfo);
    }

    /**
     * 跳转到帖子发布页面
     */
    public static void gotoPublishSubject(Context context,String title,long topicId){
        PublishSubjectActivity.gotoPublishSubject(context,title,topicId);
    }

    /**
     * 启动GPS导航. 前置条件：导航引擎初始化成功
     */
    public static void launchNavigator(double lat,double lng,final Activity context){
        //这里给出一个起终点示例，实际应用中可以通过POI检索、外部POI来源等方式获取起终点坐标
        double[] temp = bd_decrypt(TianYaGongZaiApplication.mLng, TianYaGongZaiApplication.mLat);
        double c_lat = temp[1];
        double c_lng = temp[0];

        final BNRoutePlanNode sNode = new BNRoutePlanNode(c_lng, c_lat,
                "", null, BNRoutePlanNode.CoordinateType.GCJ02);
        BNRoutePlanNode eNode  = new BNRoutePlanNode(lng, lat,
                "", null, BNRoutePlanNode.CoordinateType.GCJ02);
        if (sNode != null && eNode != null) {
            List<BNRoutePlanNode> list = new ArrayList<BNRoutePlanNode>();
            list.add(sNode);
            list.add(eNode);
            BaiduNaviManager.getInstance().launchNavigator(context, list, 1, true, new BaiduNaviManager.RoutePlanListener() {
                @Override
                public void onJumpToNavigator() {
                    Intent intent = new Intent(context, BNNavGuideActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(BNNavGuideActivity.ROUTE_PLAN_NODE, (BNRoutePlanNode) sNode);
                    intent.putExtras(bundle);
                    context.startActivity(intent);
                }

                @Override
                public void onRoutePlanFailed() {

                }
            });
        }
    }
    /**
     * 启动GPS导航. 前置条件：导航引擎初始化成功
     */
    public static void launchNavigatorBd(double lat,double lng,final Activity context,final Dialog dialog){
        //这里给出一个起终点示例，实际应用中可以通过POI检索、外部POI来源等方式获取起终点坐标
        double[] temp = bd_decrypt(TianYaGongZaiApplication.mLng, TianYaGongZaiApplication.mLat);
        double c_lat = temp[1];
        double c_lng = temp[0];

        double[] temp1 = bd_decrypt(lng, lat);
        double lat1 = temp1[1];
        double lng1 = temp1[0];

        final BNRoutePlanNode sNode = new BNRoutePlanNode(c_lng, c_lat,
                "", null, BNRoutePlanNode.CoordinateType.GCJ02);
        BNRoutePlanNode eNode  = new BNRoutePlanNode(lng1, lat1,
                "", null, BNRoutePlanNode.CoordinateType.GCJ02);
        if (sNode != null && eNode != null) {
            List<BNRoutePlanNode> list = new ArrayList<BNRoutePlanNode>();
            list.add(sNode);
            list.add(eNode);
            BaiduNaviManager.getInstance().launchNavigator(context, list, 1, true, new BaiduNaviManager.RoutePlanListener() {
                @Override
                public void onJumpToNavigator() {
                    if(dialog != null){
                        dialog.dismiss();
                    }
                    Intent intent = new Intent(context, BNNavGuideActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(BNNavGuideActivity.ROUTE_PLAN_NODE, (BNRoutePlanNode) sNode);
                    intent.putExtras(bundle);
                    context.startActivity(intent);
                }

                @Override
                public void onRoutePlanFailed() {
                    LocalUtils.showToast(context, "查找导航线路失败!");
                }
            });
        }
    }
    /**
     *  BD-09 坐标转换成GCJ-02 坐标
     * @param bd_lon  经度
     * @param bd_lat  纬度
     * @return
     */
    private static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    public static double[] bd_decrypt(double bd_lon,double bd_lat) {
        double[] d=new double[2];
        double x = bd_lon - 0.0065, y = bd_lat - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
        double gg_lon = z * Math.cos(theta);
        double gg_lat = z * Math.sin(theta);
        d[0]=gg_lon;
        d[1]=gg_lat;
        return d;
    }
}
